package com.company.cloud.club.modules.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.company.cloud.club.modules.core.util.HttpUtils;
import com.company.cloud.club.modules.core.util.ValidateUtil;
import com.company.cloud.club.modules.dto.ticket.OrderByProductSingleSaveParam;
import com.company.cloud.club.modules.dto.ticket.UserRegisterParam;
import com.company.cloud.club.modules.dto.ticket.UserUpdateParam;
import com.company.cloud.club.modules.service.TicketService;
import com.company.cloud.common.exception.BusinessException;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@Service
public class TicketServiceImpl implements TicketService {

    private static final String ACCESS_TOKEN_KEY = "a529605da0abe9d05d63e032f557a25b";

    private static final String HOST = "https://app.artshanghaifair.com";

    @Override
    public String generateAccessToken() {
        long current = System.currentTimeMillis();
        String token = ACCESS_TOKEN_KEY + "#" + current;
        byte[] encode = Base64.getEncoder().encode(token.getBytes());
        return new String(encode);
    }

    @Override
    public void validateToken(String token) {

    }

    @Override
    public String registerByMobileNotLogin(UserRegisterParam params) {
        Map<String, String> headers = new HashMap<>();
        headers.put("content-type", "application/json");
        Map<String, String> querys = new HashMap<>();
        Map<String, String> bodys = new HashMap<>();
        bodys.put("mobile", params.getMobile());
        bodys.put("nickName", params.getNickName());
        bodys.put("companyNo", "PTC");

        try {
            HttpResponse httpResponse = HttpUtils.doPost(HOST, "/api/user-center/app/user/register/ticket/save", "POST", headers, querys, JSON.toJSONString(bodys));
            String jsonStr = EntityUtils.toString(httpResponse.getEntity());
            JSONObject obj = JSON.parseObject(jsonStr);
            String code = obj.getString("code");
            if ("0".equals(code)) {
                System.out.println(obj.toJSONString());
            }
        } catch (Exception e) {
            throw new BusinessException("");
        }

        return null;
    }

    @Override
    public String updateUserInfo(UserUpdateParam params) {
        Map<String, String> headers = new HashMap<>();
        headers.put("content-type", "application/json");
        Map<String, String> querys = new HashMap<>();
        Map<String, String> bodys = new HashMap<>();
        bodys.put("realName", params.getRealName());
        bodys.put("tel", params.getPhone());
        try {
            HttpResponse httpResponse = HttpUtils.doPost(HOST, "/api/user-center/app/user/register/ticket/save", "POST", headers, querys, JSON.toJSONString(bodys));
            String jsonStr = EntityUtils.toString(httpResponse.getEntity());
            JSONObject obj = JSON.parseObject(jsonStr);
            String code = obj.getString("code");
            if ("0".equals(code)) {
                System.out.println(obj.toJSONString());
            }
        } catch (Exception e) {
            throw new BusinessException("");
        }
        return null;
    }

    @Override
    public String saveByProductSingle(OrderByProductSingleSaveParam params) {
        Map<String, String> headers = new HashMap<>();
        Map<String, String> querys = new HashMap<>();
        Map<String, String> bodys = new HashMap<>();
        try {
            HttpResponse httpResponse = HttpUtils.doPost(HOST, "/mgr/mall/order/single/save", "POST", headers, querys, JSON.toJSONString(bodys));
            String jsonStr = EntityUtils.toString(httpResponse.getEntity());
            JSONObject obj = JSON.parseObject(jsonStr);
            String code = obj.getString("code");
            if ("0".equals(code)) {
                System.out.println(obj.toJSONString());
            }
        } catch (Exception e) {
            throw new BusinessException("");
        }
        return null;
    }

    public static void main(String[] args) {
        Map<String, String> headers = new HashMap<>();
        headers.put("content-type", "application/json");
        Map<String, String> querys = new HashMap<>();
        Map<String, String> bodys = new HashMap<>();
        bodys.put("realName", "蜘蛛侠");
        bodys.put("tel", "15300000000");
        try {
            HttpResponse httpResponse = HttpUtils.doPost(HOST, "/api/user-center/app/user/register/ticket/update", "POST", headers, querys, JSON.toJSONString(bodys));
            String jsonStr = EntityUtils.toString(httpResponse.getEntity());
            JSONObject obj = JSON.parseObject(jsonStr);
            String code = obj.getString("code");
            if ("0".equals(code)) {
                System.out.println(obj.toJSONString());
            }
        } catch (Exception e) {
            throw new BusinessException("");
        }
    }

}
